//
//  XRGridTableView.m
//  XRmenu
//
//  Created by loufq on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRGridTableView.h"


@interface XRGridTableView(P)
	-(void)loadThumbView;
@end


@implementation XRGridTableView
@synthesize rows;
@synthesize columns;
@synthesize currentPageIndex;
@synthesize pageCount;
@synthesize datasource;
@synthesize delegate;
@synthesize mNavView;

#define kTagDishAlbumNavView 1000
#define kTagSortViewBegin 1001


//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{ 
	mNavView = nil;
    datasource = nil;
    delegate = nil;
    [super dealloc];
}

-(id)initWithTarget:(id)aTarget position:(CGPoint)aPosition
{
	if ((self = [super init])){
		self.frame = CGRectMake(aPosition.x, aPosition.y, 0, 0); 
		self.delegate = aTarget;
		self.datasource = aTarget;  
		 
		if ([aTarget respondsToSelector:@selector(cellSize)]) {
			cellSize = [self.datasource cellSize]; 
		} 
		if ([aTarget respondsToSelector:@selector(cellMargin)]) {
			cellMargin = [self.datasource cellMargin];
		}
		[self add1fingerLeftSwipe];
		[self add1fingerRightSwipe]; 
		//pagecontroller导航条
		NSDictionary* aDic = [self findNode:APPSESSION.allconfig withKey:@"DishAlbumNavView"];
		DishAlbumNavView* navView =[[DishAlbumNavView alloc] initWithConfig:aDic];
		navView.frame = CGRectMake(1, self.frame.size.height-29, self.frame.size.width , 30);
		navView.tag=kTagDishAlbumNavView;
		navView.delegate=self;
		self.mNavView = navView;
		[self addSubview:navView];
		[navView release]; 
	}
	return self;
}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    if ([APPSESSION.pluPlus count] == 0) {
        return;
    }
	if (currentPageIndex >=(pageCount - 1)) {//最右边
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:loc(@"提示")
                                                        message:loc(@"最后一页")
                                                       delegate:self 
                                              cancelButtonTitle:loc(@"确定")
                                              otherButtonTitles:nil];
        [alert show];     
        [alert release];
		return;
	}
    [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"trashDish.caf"]];
	
	self.mNavView.mPC.currentPage += 1; 
	currentPageIndex= self.mNavView.mPC.currentPage; 
	//清除原来的内容
	int delIndex = kTagSortViewBegin;
	while ([self viewWithTag:delIndex]) {
		[[self viewWithTag:delIndex] removeFromSuperview];
		delIndex++;
	}
	[self loadThumbView];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    if ([APPSESSION.pluPlus count] == 0) {
        return;
    }
	if (currentPageIndex <=0 ) {//最左边
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:loc(@"提示")
                                                        message:loc(@"第一页")
                                                       delegate:self 
                                              cancelButtonTitle:loc(@"确定")
                                              otherButtonTitles:nil];
        [alert show];     
        [alert release];
		return;
	}
    
    [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"trashDish.caf"]];
	//[self transitionWithDirection:kCATransitionFromLeft type:kCATransitionMoveIn];
	self.mNavView.mPC.currentPage -= 1;
	currentPageIndex= self.mNavView.mPC.currentPage;
	//清除原来的内容
	int delIndex = kTagSortViewBegin;
	while ([self viewWithTag:delIndex]) {
		[[self viewWithTag:delIndex] removeFromSuperview];
		delIndex++;
	}
	[self loadThumbView];
	//[self slideInFrom:kFTAnimationLeft duration:0.2f delegate:nil];
}


-(void)reloadData
{
	int iRowCount = [self.datasource multiColTableViewShowRows];
	int iColCount = [self.datasource multiColTableViewShowColums];
	int iAllCount =[self.datasource multiColTableViewDisplayCount]; 
	
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, iColCount*(cellSize.width +cellMargin.width), iRowCount * (cellSize.height +cellMargin.height ));
 
	self.mNavView.frame = CGRectMake(1, self.frame.size.height, self.frame.size.width-cellMargin.width   , 30);
	self.mNavView.mPC.numberOfPages = ceil(iAllCount *1.0 /(iRowCount*iColCount));
    DebugLog(@"numberPages=%d", self.mNavView.mPC.numberOfPages);
    if (iRowCount+iColCount==0) {
        self.mNavView.mPC.currentPage=0;
    }else{
        self.mNavView.mPC.currentPage = APPSESSION.currentPluIndex / (iRowCount * iColCount);//自动滚动到当前页面
    }
    DebugLog(@"iRowCount=%d,iColCount=%d", iRowCount,iColCount);
	currentPageIndex = 	self.mNavView.mPC.currentPage;
    DebugLog(@"currentPageIndex=%d", currentPageIndex);
	pageCount = self.mNavView.mPC.numberOfPages;
	
	[self loadThumbView];
	//[NSThread detachNewThreadSelector:@selector(loadThumbView) toTarget:self withObject:nil];
}

-(void)loadThumbView
{
	//基本信息
	int iRowCount = [self.datasource multiColTableViewShowRows];
	int iColCount = [self.datasource multiColTableViewShowColums];
	int iEntityStartIndex =currentPageIndex * (iRowCount * iColCount);
	//PageNav
	
	//清除原来的内容
	int delIndex = kTagSortViewBegin;
	while ([self viewWithTag:delIndex]) {
		[[self viewWithTag:delIndex] removeFromSuperview];
		delIndex++;
	}
	//设置内容
	NSInteger iIndex=0;
	int iRow = 0 ;
	int iCol = 0;
	for (iRow=0; iRow < iRowCount; iRow++) {
		for (iCol=0; iCol <iColCount; iCol++) {
			if (iEntityStartIndex >= [self.datasource multiColTableViewDisplayCount]) {
				continue;
			}

			XRGridTableCell* ev = [self.datasource multiColTableView:self viewAtPosition:iEntityStartIndex]; 
			ev.delegate = self;
			ev.tag =kTagSortViewBegin+iIndex; 
			[ev setBackgroundColor:[UIColor clearColor]];
			ev.frame = CGRectMake(iCol*(cellSize.width+cellMargin.width), 
								  iRow*(cellSize.height+cellMargin.height), 
								  cellSize.width, 
                                  cellSize.height);
			ev.delegate=self;
			[self addSubview:ev];
			
			iIndex++;
			iEntityStartIndex++;
		} 
	}
}

-(void)dishAlbumNavView:(DishAlbumNavView*)navView perIndex:(NSInteger)perIndex changToIndex:(NSInteger)aIndex
{
	
	
}

-(void)multiColTableCellTapped:(XRGridTableCell*)aCell
{
	if (self.delegate) {
		int iRowCount = [self.datasource multiColTableViewShowRows];
		int iColCount = [self.datasource multiColTableViewShowColums];
		int pos = currentPageIndex*(iRowCount*iColCount) + aCell.tag-kTagSortViewBegin;	
		[self.delegate multiColTableView:self didTappedAtPosition:pos withCell:aCell]; 
	}
}
@end
